<?php

namespace App\Controller\App;

use App\Base\BaseCache;
use App\Base\BaseController4Restful;
use App\Base\Format;
use App\Entity\User;
use App\Model\LazyCommon\UserModel;
use App\Repository\CategoryRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/app")
 */
class UserController extends BaseController4Restful
{
    /**
     * @Route("/user/home", name="AppUserHome")
     */
    public function AppUserHome()
    {
        $user = $this->getUser();
        $this->setGroups("home");

        return $this->json_out(200,"数据获取成功",$user);
    }

    /**
     * @Route("/user/info", name="AppUserInfo")
     */
    public function AppUserInfo()
    {
        $user = $this->needToBeUser();
        $data = $this->getRequestData();
        $User = $this->updateEntity($data,User::class,$user);
//        if(!$avatar = $this->getParamRequired("avatar")){return $this->json_out(400,"请设置头像");}
//        if(!$realName = $this->getParamRequired("realName")){return $this->json_out(400,"填写真实姓名");}
//        if(!$name = $this->getParamRequired("name")){return $this->json_out(400,"请设置昵称");}
//        if(!$sex = $this->getParamRequired("sex")){return $this->json_out(400,"请设置性别");}
//        if(!$age = $this->getParamRequired("age")){return $this->json_out(400,"请填写年龄");}
//        if(!$career = $this->getParamRequired("career")){return $this->json_out(400,"请填写您的职业");}

//        $user->setAvatar($avatar)->setName($name)
//            ->setRealname($realName)
//            ->setSex($sex)
//            ->setAge($age)
//            ->setCareer($career)
//        ;
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        $this->setGroups("user");
        return $this->json_out(200,"修改成功",$user);
    }



    /**
     * @Route("/user/password", name="AppUserInfoPassword",methods={"GET"})
     */
    public function AppUserInfoPassword()
    {
        $user = $this->needToBeUser();
        if(!$old = $this->getParamRequired("old")){return $this->json_out(400,"请输入原密码");}
        if(!$new = $this->getParamRequired("new")){return $this->json_out(400,"请输入新密码");}
        if (md5($old) != $user->getPassword() ) {
            return $this->json_out(400,"密码错误");
        }

        $user->setPassword(md5($new));

        $this->myEm()->persist($user);
        $this->myEm()->flush();
        $this->setGroups("user");
        return $this->json_out(200,"修改成功",$user);
    }



}
